{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from hmmlearn import hmm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 设定隐藏状态的集合\n",
    "states = [\"box1\", \"box2\", \"box3\"]\n",
    "n_states = len(states)\n",
    "\n",
    "# 设定观察状态的集合\n",
    "observations = [\"red\", \"white\"]\n",
    "n_observations = len(observations)\n",
    "\n",
    "# 设置初始状态分布\n",
    "start_probability = np.array([0.2, 0.4, 0.4])\n",
    "\n",
    "# 设置状态转移概率分布矩阵\n",
    "transition_probability = np.array([\n",
    "    [0.5, 0.2, 0.3],\n",
    "    [0.3, 0.5, 0.2],\n",
    "    [0.2, 0.3, 0.5]\n",
    "])\n",
    "\n",
    "# 设置观测状态概率矩阵\n",
    "emission_probability = np.array([\n",
    "    [0.5, 0.5],\n",
    "    [0.4, 0.6],\n",
    "    [0.7, 0.3]\n",
    "])\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1798afbd84713782"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 设定模型参数\n",
    "model = hmm.MultinomialHMM(n_components=n_states)\n",
    "# 设定初始状态分布\n",
    "model.startprob_ = start_probability\n",
    "# 设定状态转移概率矩阵\n",
    "model.transmat_ = transition_probability\n",
    "# 设定观测状态概率矩阵\n",
    "model.emissionprob_ = emission_probability"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "539ee9b74a78ebe6"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 设定观测序列 observations的下标index\n",
    "seen = np.array([[0, 1, 0]]).T\n",
    "seen"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a0f4f98237166f09"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "observations"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9d567767e0bec705"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# seen是二维数组，observations是一维数组，所以要把seen二维转一维数组\n",
    "seen.flatten() "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "323d649c402a8f78"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\",\".join(map(lambda x: observations[x], seen.flatten()))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "50782ab57d509701"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(\"球的观察顺序=\", \",\".join(map(lambda x: observations[x], seen.flatten())))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fac2053a40ce05a5"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 维特比模型训练\n",
    "box = model.predict(seen)\n",
    "box"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b23a6fd3d716636d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(\"盒子最可能的隐藏状态顺序=\", \",\".join(map(lambda x: states[x], box)))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c96db7c28aabb57"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "score=model.score(seen) #输出的是一个-2.038545309915233....需要用"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c45248580dea95dd"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import math \n",
    "math.exp(score) #最后得出概率值 0.130218....."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8eac33d4c747c954"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
